package com.bubblegumproject.ogo.swing.util;

import com.bubblegumproject.ogo.swing.events.ConditionActionMap;
import static com.bubblegumproject.ogo.swing.events.MenuItemClickedCondition.MenuItemClicked;

import javax.swing.*;

/**
 * Builder for JMenuBar objects.
 *
 * @author Azubuko Obele (buko.obele@gmail.com)
 */
public class MenuBarBuilder {

    private JMenuBar menuBar;

    private ConditionActionMap conditions;

    public MenuBarBuilder(ConditionActionMap conditions) {
        assert(conditions != null);
        menuBar = new JMenuBar();
        this.conditions = conditions;
    }

    public MenuBarBuilder menubar() {
        return this;
    }

    public MenuBuilder menu(String menuName, int mnemonic) {
        return new MenuBuilder(menuName, mnemonic);
    }

    public JMenuBar result() {
        return menuBar;
    }

    public class MenuBuilder {

        private JMenu menu;

        private MenuBuilder(String menuName, int mnemonic) {
            assert(menuName != null);
            menu = new JMenu(menuName);
            menu.setMnemonic(mnemonic);
            menuBar.add(menu);
        }

        public MenuBuilder item(String name, int mnemonic, KeyStroke accelerator,
                         com.bubblegumproject.ogo.action.Action action) {
            JMenuItem item = new JMenuItem(name, mnemonic);
            if (accelerator != null) item.setAccelerator(accelerator);
            conditions.map(action, MenuItemClicked(item));
            menu.add(item);
            return this;
        }
    }
}
